<template>
  <div class="financial-balance">
    <vertical-table :page="pageIndex + '/' + pageCount" class="table" :label-width="215" :labels="labels" :coldata="colData" @lastpage="handleLastPage" @nextpage="handleNextPage" @edit="handleEdit"></vertical-table>
  </div>
</template>

<script>
import { requestAjax } from '../../common.js'
import CONSTANT from '../../constant/constant.js'

export default {
  data() {
    return {
      labels: [
        {
          label: 'rptMaster',
          name: '报表主体',
        },
        {
          label: 'isCheck',
          name: '是否审计'
        },
        {
          label: 'creatorName',
          name: '报表录入人'
        },
        {
          label: 'currencyDesc',
          name: '报表币种'
        },
        {
          label: 'attachment',
          name: '报表附件',
          event: 'download'
        },
        {
          label: '',
          dark: true
        },
        {
          label: 'bankCash',
          name: '货币资金'
        },
        {
          label: 'tradableFinAssets',
          name: '交易性金融资产'
        },
        {
          label: 'notesReceivable',
          name: '应收票据'
        },
        {
          label: 'accountReceivable',
          name: '应收账款'
        },
        {
          label: 'otherReceivable',
          name: '其他应收款'
        },
        {
          label: 'prepayment',
          name: '预付账款'
        },
        {
          label: 'dvdRcv',
          name: '应收股利'
        },
        {
          label: 'intRcv',
          name: '应收利息'
        },
        {
          label: 'redMonetaryCapForSale',
          name: '买入返售金融资产'
        },
        {
          label: 'inventory',
          name: '存货'
        },
        {
          label: 'consumpitveBioAssets',
          name: '其中：消耗性生物资产'
        },
        {
          label: 'deferredExp',
          name: '待摊费用'
        },
        {
          label: 'hfsAssets',
          name: '划分为持有待售的资产'
        },
        {
          label: 'nonCurAssetsDueWithin1y',
          name: '一年内到期的非流动资产'
        },
        {
          label: 'otherAssets',
          name: '其他流动资产'
        },
        {
          label: 'currentAssets',
          name: '流动资产合计',
          dark: true
        },
        {
          label: 'finAssetsAvailForSale',
          name: '可供出售金融资产'
        },
        {
          label: 'heldToMtyInvest',
          name: '持有至到期投资'
        },
        {
          label: 'investRealEstate',
          name: '投资性房地产'
        },
        {
          label: 'longtermInvestment',
          name: '长期股权投资'
        },
        {
          label: 'longTermRec',
          name: '长期应收款'
        },
        {
          label: 'netBal',
          name: '固定资产'
        },
        {
          label: 'projMatl',
          name: '工程物资'
        },
        {
          label: 'constructionInprogress',
          name: '在建工程'
        },
        {
          label: 'fixAssetsDisp',
          name: '固定资产清理'
        },
        {
          label: 'productiveBioAssets',
          name: '生产性生物资产'
        },
        {
          label: 'oilAndNaturalGasAssets',
          name: '油气资产'
        },
        {
          label: 'intangibleAssets',
          name: '无形资产'
        },
        {
          label: 'rAndDCosts',
          name: '开发支出'
        },
        {
          label: 'goodwill',
          name: '商誉'
        },
        {
          label: 'longtermDeferredExpense',
          name: '长期待摊费用'
        },
        {
          label: 'deferredTaxAssets',
          name: '递延所得税资产'
        },
        {
          label: 'othNonCurAssets',
          name: '其他非流动资产'
        },
        {
          label: 'totNonCurAssets',
          name: '非流动资产合计',
          dark: true
        },
        {
          label: 'totAssets',
          name: '资产总计',
          dark: true
        },
        {
          label: 'shorttermLoans',
          name: '短期借款'
        },
        {
          label: 'tradableFinLiab',
          name: '交易性金融负债'
        },
        {
          label: 'notesPayable',
          name: '应付票据'
        },
        {
          label: 'acctPayable',
          name: '应付账款'
        },
        {
          label: 'advanceFromCustomers',
          name: '预收账款'
        },
        {
          label: 'emplBenPayable',
          name: '应付职工薪酬'
        },
        {
          label: 'taxesPayable',
          name: '应交税费'
        },
        {
          label: 'intPayable',
          name: '应付利息'
        },
        {
          label: 'dvdPayable',
          name: '应付股利'
        },
        {
          label: 'otherPayable',
          name: '其他应付款'
        },
        {
          label: 'accExp',
          name: '预提费用'
        },
        {
          label: 'deferredIncCurLiab',
          name: '递延收益-流动负债'
        },
        {
          label: 'hfsLiab',
          name: '划分为持有待售的负债'
        },
        {
          label: 'nonCurLiabDueWithin1y',
          name: '一年内到期的非流动负债'
        },
        {
          label: 'otherCurrentLiability',
          name: '其他流动负债'
        },
        {
          label: 'totalCurrentLiability',
          name: '流动负债合计',
          dark: true
        },
        {
          label: 'longtermLoans',
          name: '长期借款'
        },
        {
          label: 'bondsPayable',
          name: '应付债券'
        },
        {
          label: 'ltPayable',
          name: '长期应付款'
        },
        {
          label: 'ltEmplBenPayable',
          name: '长期应付职工薪酬'
        },
        {
          label: 'specificItemPayable',
          name: '专项应付款'
        },
        {
          label: 'provisions',
          name: '预计负债'
        },
        {
          label: 'deferredTaxLiab',
          name: '递延所得税负债'
        },
        {
          label: 'deferredIncNonCurLiab',
          name: '递延收益-非流动负债'
        },
        {
          label: 'othNonCurLiab',
          name: '其他非流动负债'
        },
        {
          label: 'totNonCurLiab',
          name: '非流动负债合计',
          dark: true
        },
        {
          label: 'totLiab',
          name: '负债合计',
          dark: true
        },
        {
          label: 'netBa',
          name: '实收资本（或股本）'
        },
        {
          label: 'capitalReserves',
          name: '资本公积'
        },
        {
          label: 'surplusReserves',
          name: '盈余公积'
        },
        {
          label: 'retainedProfits',
          name: '未分配利润'
        },
        {
          label: 'tsyStk',
          name: '库存股'
        },
        {
          label: 'otherComprehIncBs',
          name: '其他综合收益'
        },
        {
          label: 'specialRsrv',
          name: '专项储备'
        },
        {
          label: 'provNomRisks',
          name: '一般风险准备'
        },
        {
          label: 'cnvdDiffForeignCurrStat',
          name: '外币报表折算差额'
        },
        {
          label: 'unconfirmedInvestLossBs',
          name: '未确认的投资损失'
        },
        {
          label: 'minorityInt',
          name: '少数股东权益'
        },
        {
          label: 'eqyBelongtoParcomsh',
          name: '归属于母公司股东的权益'
        },
        {
          label: 'totEquity',
          name: '所有者权益合计',
          dark: true
        },
        {
          label: 'totLiabShrhldrEqy',
          name: '负债及股东权益合计',
          dark: true
        }
      ],
      colData: [ {}, {}, {} ],
      pageCount: 1,
      pageIndex: 1
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    loadData(index = 1) {
      const url = CONSTANT.URL.COMPANY.FINANCIALBALANCESHEET
      const params = {
        pageIndex: index,
        pageSize: 3,
        userId: this.$router.currentRoute.query.id,
        associationId:this.$router.currentRoute.query.userId || 0
      }
      requestAjax(url, '{}', params, res => {
        this.pageCount = res.data.pageCount
        this.pageIndex = index
        if (!res.data.bussData) return
        this.colData = res.data.bussData.map(i => {
          let totalNum = {
              bankCash:i.bankCash,
              tradableFinAssets:i.tradableFinAssets,
              notesReceivable:i.notesReceivable,
              accountReceivable:i.accountReceivable,
              otherReceivable:i.otherReceivable,
              prepayment:i.prepayment,
              dvdRcv:i.dvdRcv,
              intRcv:i.intRcv,
              redMonetaryCapForSale:i.redMonetaryCapForSale,
              inventory:i.inventory,
              consumpitveBioAssets:i.consumpitveBioAssets,
              deferredExp:i.deferredExp,
              hfsAssets:i.hfsAssets,
              nonCurAssetsDueWithin1y:i.nonCurAssetsDueWithin1y,
              otherAssets:i.otherAssets
          }
          let currentAssets = 0;
          for (let i in totalNum) {
            if (!isNaN(totalNum[i]) && totalNum[i]) {
                   currentAssets += Number(totalNum[i])
            }
          }
          totalNum = {
              finAssetsAvailForSale:i.finAssetsAvailForSale,
              heldToMtyInvest:i.heldToMtyInvest,
              investRealEstate:i.investRealEstate,
              longtermInvestment:i.longtermInvestment,
              longTermRec:i.longTermRec,
              fixAssetsDisp:i.fixAssetsDisp,
              projMatl:i.projMatl,
              netBal:i.netBal,
              constructionInprogress:i.constructionInprogress,
              fixAssetsDisp:i.fixAssetsDisp,
              productiveBioAssets:i.productiveBioAssets,
              oilAndNaturalGasAssets:i.oilAndNaturalGasAssets,
              intangibleAssets:i.intangibleAssets,
              rAndDCosts:i.rAndDCosts,
              goodwill:i.goodwill,
              longtermDeferredExpense:i.longtermDeferredExpense,
              deferredTaxAssets:i.deferredTaxAssets,
              othNonCurAssets:i.othNonCurAssets
          }
          let totNonCurAssets = 0;
          for (let i in totalNum) {
            if (!isNaN(totalNum[i]) && totalNum[i]) {
                   totNonCurAssets += Number(totalNum[i])
            }
          }
          totalNum = {
              shorttermLoans:i.shorttermLoans,
              tradableFinLiab:i.tradableFinLiab,
              notesPayable:i.notesPayable,
              acctPayable:i.acctPayable,
              advanceFromCustomers:i.advanceFromCustomers,
              emplBenPayable:i.emplBenPayable,
              taxesPayable:i.taxesPayable,
              intPayable:i.intPayable,
              dvdPayable:i.dvdPayable,
              otherPayable:i.otherPayable,
              accExp:i.accExp,
              deferredIncCurLiab:i.deferredIncCurLiab,
              hfsLiab:i.hfsLiab,
              nonCurLiabDueWithin1y:i.nonCurLiabDueWithin1y,
              otherCurrentLiability:i.otherCurrentLiability,
          }
          let totalCurrentLiability = 0;
          for (let i in totalNum) {
            if (!isNaN(totalNum[i]) && totalNum[i]) {
                   totalCurrentLiability += Number(totalNum[i])
            }
          }
          //非流动负债合计
          totalNum = {
              longtermLoans:i.longtermLoans,
              bondsPayable:i.bondsPayable,
              ltPayable:i.ltPayable,
              ltEmplBenPayable:i.ltEmplBenPayable,
              specificItemPayable:i.specificItemPayable,
              provisions:i.provisions,
              deferredTaxLiab:i.deferredTaxLiab,
              deferredIncNonCurLiab:i.deferredIncNonCurLiab,
              othNonCurLiab:i.othNonCurLiab
          }
          let totNonCurLiab = 0;
          for (let i in totalNum) {
            if (!isNaN(totalNum[i]) && totalNum[i]) {
                   totNonCurLiab += Number(totalNum[i])
            }
          }
          //所有者权益合计
          totalNum = {
              netBa:i.netBa,
              capitalReserves:i.capitalReserves,
              surplusReserves:i.surplusReserves,
              retainedProfits:i.retainedProfits,
              tsyStk:i.tsyStk,
              otherComprehIncBs:i.otherComprehIncBs,
              specialRsrv:i.specialRsrv,
              provNomRisks:i.provNomRisks,
              cnvdDiffForeignCurrStat:i.cnvdDiffForeignCurrStat,
              unconfirmedInvestLossBs:i.unconfirmedInvestLossBs,
              minorityInt:i.minorityInt,
              eqyBelongtoParcomsh:i.eqyBelongtoParcomsh
          }
          let totEquity = 0;
          for (let i in totalNum) {
            if (!isNaN(totalNum[i]) && totalNum[i]) {
                   totEquity += Number(totalNum[i])
            }
          }
          return {
            header: {
              text: i.rptDate,
              opText: '编辑',
              opEvent: 'edit'
            },
            data: {
              ...i,
              attachment: i.attachmentId ? {
                text: i.attachmentFile.fileName,
                color: '#0087DC',
                download: i.attachmentFile.fileUrl
              } : {
                text: '上传补充详细附件',
                color: '#ED7018',
                event: 'edit'
              },
              isCheck: i.isCheck == 'y' ? '是' : '否',
              currentAssets,
              totNonCurAssets,
              totalCurrentLiability,
              totNonCurLiab,
              totEquity,
              totLiab: totNonCurLiab + totalCurrentLiability,
              totAssets: currentAssets + totNonCurAssets,
              totLiabShrhldrEqy: totNonCurLiab + totalCurrentLiability + totEquity
            }
          }
        })
        while(this.colData.length < 3) {
          this.colData.push([{header: {}, data: {}}])
        }
      })
    },
    handleLastPage() {
      if (this.pageIndex <= 1) {
        this.$message.warning('已经是第一页了')
      } else {
        this.loadData(this.pageIndex - 1)
      }
    },
    handleNextPage() {
      if (this.pageIndex >= this.pageCount) {
        this.$message.warning('已经是最后一页了')
      } else {
        this.loadData(this.pageIndex + 1)
      }
    },
    handleEdit(item) {
      this.$router.push({
        path: '/partner/financialInfo_v2/financialBalanceEdit', 
        query: {
          id: item.data.id,
          userId: item.data.userId,
          name: this.$route.query.name
        }
      })
    }
  },
  components: {
    "vertical-table": () => import("../common/vertical-table.vue")
  }
}
</script>

<style scoped>
.table {
  margin-right: 40px;
}
</style>
